#include "dlgattributi.h"









DLGAttributi::DLGAttributi( DB_Manager * dbin , int catessss , QString Exists_Atts )
{
	  resize(433, 364);
	  setAcceptDrops(true);
	  const QString StreamIcone = dbin->Get_Setting(_NR_ICONE_ATTRIBUTI_);
	  title = tr("Attributi categoria %1 CTRL multiselezione.").arg(catessss);
    setWindowTitle(title);
	  TabIcone ddOggetto = OpenTabelicon(StreamIcone); 
	  li =  ddOggetto.all_lines;
	  qDebug() << "### size dls " << li.size();
	  qDebug() << "### CountCates " << CountCates(catessss);
    gridLayout = new QGridLayout(this);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    groupBox = new QGroupBox(title,this);
    gridLayout1 = new QGridLayout(groupBox);
    hboxLayout = new QHBoxLayout();
    master = new IconScroller(true,groupBox);
	  hboxLayout->addWidget(master);
    vboxLayout = new QVBoxLayout();
    xdelete = new QToolButton(groupBox);
    xdelete->setIcon(QIcon(QString::fromUtf8(":/default/img/default/delete.png")));
    xdelete->setIconSize(QSize(20, 20));
	
    xdelete->setToolTip (tr("Rimuovi")); 
    vboxLayout->addWidget(xdelete);
    xappend = new QToolButton(groupBox);
    xappend->setIcon(QIcon(QString::fromUtf8(":/default/img/default/append_data.png")));
    xappend->setIconSize(QSize(20, 20));
		xappend->setToolTip (tr("Appendi"));

    vboxLayout->addWidget(xappend);
    spacerItem = new QSpacerItem(20, 61, QSizePolicy::Minimum, QSizePolicy::Expanding);
    vboxLayout->addItem(spacerItem);
    hboxLayout->addLayout(vboxLayout);
    slave = new IconScroller(false,groupBox);
    hboxLayout->addWidget(slave);
    gridLayout1->addLayout(hboxLayout, 0, 0, 1, 1);
    gridLayout->addWidget(groupBox, 0, 0, 1, 1);
    hboxLayout1 = new QHBoxLayout();
    spacerItem1 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    hboxLayout1->addItem(spacerItem1);
    accetta = new QPushButton(this);
    accetta->setIcon(QIcon(QString::fromUtf8(":/save/img/save/save-48x48.png")));
    hboxLayout1->addWidget(accetta);
    Rigetta = new QPushButton(this);
    hboxLayout1->addWidget(Rigetta);
    gridLayout->addLayout(hboxLayout1, 1, 0, 1, 1);
		accetta->setText(tr("Ok"));
    Rigetta->setText(tr("Chiudi"));
		connect(accetta, SIGNAL(clicked()), this, SLOT(accept()));
    connect(Rigetta, SIGNAL(clicked()), this, SLOT(reject()));
    
    LoadConfig(catessss);
    
		QStringList list = Exists_Atts.split("/",QString::SkipEmptyParts);
		fillUserSetting(list);
		connect(master, SIGNAL(clicked(const QModelIndex &)),this, SLOT(MActivate(const QModelIndex&)));
		connect(slave, SIGNAL(clicked(const QModelIndex &)),this, SLOT(MActivate(const QModelIndex&)));
		//////////////QMetaObject::connectSlotsByName(this);
		connect(xappend, SIGNAL(clicked()),this, SLOT(on_xappend_triggered()));
		connect(xdelete, SIGNAL(clicked()),this, SLOT(on_xdelete_triggered()));
		connect(slave, SIGNAL(AppendList(TabIcone)),this, SLOT(InsertList(TabIcone)));
}


void DLGAttributi::MActivate( const QModelIndex &index ) {
        if (!index.isValid()) {
        return;  
        }
        Master_index = index;
}

void DLGAttributi::SActivate( const QModelIndex &index ) {
        if (!index.isValid()) {
        return;  
        }
        Slave_index = index;
}


void DLGAttributi::on_xappend_triggered()
{
	      ////////////qDebug() << "### on_xappend_clicked";
	
	      const QModelIndex index = master->currentIndex();
	      if (!index.isValid()) {
        return;  
        }
				
				QStringList ListaDelleIcone = Lists();
				
					 QStringList setterLi = Lists();
				   QList<QListWidgetItem *> tutti = master->selectedItems();
						 for (int i=0; i<tutti.size(); i++) {
								QListWidgetItem *pane = tutti[i];
								IcoLine Io = pane->data(Qt::UserRole).value<IcoLine>();
							  if ( !HavingItem( Io.id() ) ) {
							    const QString Oskill = QString("%1").arg(Io.id());
									ListaDelleIcone.append(Oskill);
								}
							
						 }
				fillUserSetting( ListaDelleIcone );
}

void DLGAttributi::InsertList( TabIcone e )
{
	     QStringList ListaDelleIcone = Lists();
	     qDebug() << "### d " << e.all_lines.size();
	
	      for (int i=0; i<e.all_lines.size(); i++) {
		       IcoLine placehere = e.all_lines[i];
					 if (!HavingItem(placehere.id())) {
						 qDebug() << "### append e " << placehere.id();
		         ListaDelleIcone.append( QString("%1").arg(placehere.id()) );
					 }
					
	      }
				fillUserSetting( ListaDelleIcone );
	
}



void DLGAttributi::on_xdelete_triggered()
{
	      const QModelIndex index = slave->currentIndex();
	      if (!index.isValid()) {
        return;  
        }
				
				 QStringList setterLi = Lists();
				 QList<QListWidgetItem *> tutti = slave->selectedItems();
						 for (int i=0; i<tutti.size(); i++) {
								QListWidgetItem *pane = tutti[i];
								IcoLine Io = pane->data(Qt::UserRole).value<IcoLine>();
							  const QString Oskill = QString("%1").arg(Io.id());
							  int rem = setterLi.indexOf(Oskill);
                setterLi.removeAt(rem);
						 }
						 
				fillUserSetting( setterLi );
}


bool DLGAttributi::HavingItem( const int chars )
{
	  for (int e = 0; e < slave->model()->rowCount(); ++e) {
			     const QModelIndex index = slave->model()->index(e,0);
			     IcoLine Io = index.data(Qt::UserRole).value<IcoLine>();
			     if (Io.id() == chars ) {
						 return true;
					 }
		}
	return false;
}

QStringList DLGAttributi::Lists()
{
	QStringList myitem;
	myitem.clear();
	
	  for (int e = 0; e < slave->model()->rowCount(); ++e) {
			     const QModelIndex index = slave->model()->index(e,0);
			     IcoLine Io = index.data(Qt::UserRole).value<IcoLine>();
			     if (Io.id() > 0) {
						 myitem.append(QString("%1").arg(Io.id()));
					 }
		}
		
	return myitem;
}

int DLGAttributi::CountCates( const int kat )
{
  int summe = 0; 
  for (int i = 0; i < li.size(); ++i) {
        IcoLine Celinea = li[i];
      if (Celinea.cate = kat) {
           summe++;
      }	 
  }
  return summe;
}

void DLGAttributi::fillUserSetting( QStringList list )
{
	slave->clear();
  QString keys;
   for (int x = 0; x < list.size(); ++x)  {
     QString since = QString("| _%1_ |").arg(list.at(x));
     keys.append(since);
   }
	
  for (int i = 0; i < li.size(); ++i) {
        IcoLine Celinea = li[i];
      if (keys.contains(Celinea.Sid())) {
            QListWidgetItem *item = new QListWidgetItem(Celinea.it);
            item->setData(Qt::DecorationRole,Celinea.pix());
            item->setData(Qt::UserRole,Celinea);
				    slave->insertItem(slave->model()->rowCount(),item);
      }	 
  }
			
			
	slave->setIconSize( QSize(30,30));
	
}



void DLGAttributi::LoadConfig( const int kategoria )
{
	master->clear();
  
  int summer = CountCates(kategoria);
	
	if ( summer > 3 ) {
		
	} else {
		return;
	}
 
  for (int i = 0; i < li.size(); ++i) {
    
        IcoLine Celinea = li[i];
    
      
          if ( Celinea.cate ==  kategoria ) {
                QListWidgetItem *item = new QListWidgetItem(Celinea.it);
                item->setData(Qt::DecorationRole,Celinea.pix());
                item->setData(Qt::UserRole,Celinea);
						    
                /////item->setDragEnabled(true);
                master->insertItem(master->model()->rowCount(),item);
						    
          }	 
      
  }
	master->setIconSize( QSize(30,30));
	
}




























